package masterjava.common.client.gwt.rpc;

import com.google.gwt.user.client.rpc.AsyncCallback;
import masterjava.common.shared.util.SharedUtil;

import java.util.List;

/**
 * User: Grigory Kislin
 * Date: 29/06/11
 */
public class CallbackFactory {

    public static <T> AsyncCallback<List<T>> wrapFirstItemCallback(final AsyncCallback<T> callback) {
        return new AsyncCallback<List<T>>() {

            @Override
            public void onFailure(Throwable caught) {
                callback.onFailure(caught);
            }

            @Override
            public void onSuccess(List<T> result) {
                callback.onSuccess(SharedUtil.isEmpty(result) ? null : result.get(0));
            }
        };
    }
}
